package com.umeox.iqibla.custom_tasbih.adapter

import android.annotation.SuppressLint
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.load.engine.DiskCacheStrategy
import com.bumptech.glide.request.RequestOptions
import com.umeox.iqibla.custom_tasbih.R
import com.umeox.iqibla.custom_tasbih.base.GlideApp
import com.umeox.moto.watch.themes.widget.ImageView
import com.umeox.moto.watch.themes.widget.RoundedImageView
import com.umeox.moto.watch.themes.widget.TextView
import com.umeox.watch.moto.apiclient.model.FriendChantsCounterDTO
import java.util.Locale

class CustomTasbihRankingAdapter : RecyclerView.Adapter<CustomTasbihRankingAdapter.Holder>() {
    //判断是否全为0
    var isAllZero: Boolean = true

    private val friendChantsCounterDTOArrayList = ArrayList<FriendChantsCounterDTO>()

    fun setFriendChantsCounterDTOList(list: List<FriendChantsCounterDTO>) {
        if (list.isEmpty()) {
            return
        }
        friendChantsCounterDTOArrayList.clear()
        friendChantsCounterDTOArrayList.addAll(list)
        for (friendStepDTO in friendChantsCounterDTOArrayList) {
            if (friendStepDTO.amount != 0) {
                isAllZero = false
                break
            }
        }
        notifyDataSetChanged()
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): Holder {
        val view = LayoutInflater.from(parent.context)
            .inflate(R.layout.item_custom_tasbih_ranking_layout, parent, false)
        return Holder(view)
    }

    override fun onBindViewHolder(holder: Holder, position: Int) {
        holder.bind(position, friendChantsCounterDTOArrayList[position])
    }

    override fun getItemCount(): Int {
        return friendChantsCounterDTOArrayList.size
    }

    inner class Holder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        private val ivAvatar: RoundedImageView =
            itemView.findViewById(R.id.iv_avatar)
        private val tvName: TextView =
            itemView.findViewById(R.id.tv_name)
        private val tvNumber: TextView =
            itemView.findViewById(R.id.tv_number)
        private val tvRanking: TextView =
            itemView.findViewById(R.id.tv_ranking)
        private val ivRanking: ImageView =
            itemView.findViewById(R.id.iv_ranking)


        @SuppressLint("SetTextI18n")
        fun bind(position: Int, friendChantsCounterDTO: FriendChantsCounterDTO) {
            if (isAllZero) {
                tvRanking.visibility = View.INVISIBLE
                ivRanking.visibility = View.INVISIBLE
            } else {
                if (position < 3) {
                    tvRanking.visibility = View.INVISIBLE
                    ivRanking.visibility = View.VISIBLE
                    ivRanking.setImageLevel(position)
                } else {
                    tvRanking.visibility = View.VISIBLE
                    ivRanking.visibility = View.INVISIBLE
                    tvRanking.text = String.format(Locale.ENGLISH, "%d", position + 1)
                }
            }

            tvName.text = friendChantsCounterDTO.name
            tvNumber.text = "" + friendChantsCounterDTO.amount
            showAvatar(ivAvatar, friendChantsCounterDTO.avatar)
        }

        private fun showAvatar(ivAvatar: ImageView, photoUrl: String) {
            GlideApp.with(ivAvatar.context)
                .load(photoUrl)
                .placeholder(R.drawable.ic_default_avatar_48)
                .error(R.drawable.ic_default_avatar_48)
                .apply(
                    RequestOptions.overrideOf(48).centerCrop()
                        .diskCacheStrategy(DiskCacheStrategy.ALL)
                )
                .into(ivAvatar)
        }
    }
}
